'use strict';
var path = require('path');
/**
 * electron 主文件
 */
const  { app, BrowserWindow ,session  }  = require('electron');
let win;
function createWin() {
    // 创建浏览器窗口
    win = new BrowserWindow({
        width:1920,
        height:1080,
        useContentSize:true,
        webPreferences: {
            nodeIntegrationInWorker :true,
            nodeIntegration: true,
            contextIsolation: false,
        },
    });
    console.log(app.isPackaged);
    console.log(process.env.PORT);
    const URL = app.isPackaged
        ? path.join(__dirname, '../render/index.html') // vite 构建后的静态文件地址
        : `http://localhost:3100`; // vite 启动的服务器地址
    app.isPackaged ? win?.loadFile(URL) : win?.loadURL(URL);
}
app.whenReady().then(() => {
    app.on('activate',  ()=>{
        // On macOS it's common to re-create a window in the app when the
        // dock icon is clicked and there are no other windows open.
        if (BrowserWindow.getAllWindows().length === 0) createWin();
    });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed',  ()=>{
    if (process.platform !== 'darwin') app.quit();
});

// /
// *此处为安装vue-devtools 扩展,vue3的vue-devtools需要使用vue-devtools beta版本
// 官方文档 http://www.electronjs.org/docs/tutorial/devtools-extension
//     安装之后若提示警告，可更改vue-devtools项目中的manifest.json，删除掉与报错内容有关的内容即可
// */
app.on('ready',async ()=>{
    // if(!app.isPackaged){
    //     //install vue devtool
    //     try {
    //         const devpath = 'D:/devtools';
    //         await session.defaultSession.loadExtension(devpath);
    //     } catch (e) {
    //         console.error('Vue Devtools failed to install:', e);
    //     }
    // }
    createWin();
});
